# -*- coding: utf-8 -*-

import logging
import json
from common.bank import db as bank
from common.exemgt import db as exemgt
from common.exemgt import handler as exemgt_handler
from common.utils.decorator import response_wrapper
from django.views.decorators.http import require_POST, require_GET
from common.admin import db as admin_user
from common.admin.model import ACTION
from common.utils.ip_to_city import get_city
from datetime import datetime, timedelta

_LOGGER = logging.getLogger('hydra')


record = {
          'resource': 'exemgt/',
          'resource_id': '',
          'action': '',
          'content': '',
          'ip': '',
          'city': '',
          'operator': 1
          }


@require_GET
@response_wrapper
def get_bank(request):
    query_dct = request.GET.dict()
    data = bank.get_bank(query_dct['id'])
    return data


@require_GET
@response_wrapper
def get_exemgt(request):
    query_dct = request.GET.dict()
    if query_dct['id']:
        data = exemgt.get_exemgt(query_dct['id'])
    else:
        data = {'err': 'no parameter id found'}
    return data


@require_POST
@response_wrapper
def upload_exe(request):
    file = request.FILES['file']
    details = request.POST.dict()
    username = admin_user.get_user(details['user_id'])
    username = username.as_dict()
    details['updated_by'] = username['nickname']
    details['created_at'] = datetime.utcnow()
    if details:
        details['status'] = 0
    record['action'] = ACTION[request.method]
    record['content'] = str(details)
    record['ip'] = request.ip
    record['city'] = get_city(request.ip)
    admin_user.insert_record(record)
    insert_detail = exemgt.upsert_exemgt(details)
    result = exemgt_handler.upload_exe(file, details)
    return {'result': result, 'info_inserted': insert_detail.as_dict()}


@require_GET
@response_wrapper
def download_exe(request):
    _LOGGER.info('download: %s', request)
    filename = request.GET.dict()['filename']
    record['action'] = ACTION[request.method]
    record['content'] = str(filename)
    record['ip'] = request.ip
    record['city'] = get_city(request.ip)
    admin_user.insert_record(record)
    zip = exemgt_handler.download_exe(filename)
    return zip


@require_POST
@response_wrapper
def update_status(request):

    _LOGGER.info('update status: %s', request.body)
    id = request.POST.dict().keys()[0]
    q_dict = json.loads(id)
    data = exemgt.get_exemgt(q_dict['id'])
    username = admin_user.get_user(q_dict['user_id'])
    username = username.as_dict()
    data['updated_by'] = username['nickname']
    if 'status' in data:
        new_status = 0
        if data['status'] == 0:
            new_status = 1
        data['status'] = new_status
        new_data = exemgt.upsert_exemgt(data)
        record['action'] = ACTION[request.method]
        record['content'] = str(new_data.as_dict())
        record['ip'] = request.ip
        record['city'] = get_city(request.ip)
        admin_user.insert_record(record)
        return new_data.as_dict()
    else:
        return {'result': data}


@require_GET
@response_wrapper
def delete_exemgt(request):
    id = request.GET.dict()['id']
    _LOGGER.info('delete exe: %s', request)
    data = exemgt.delete_exemgt(id)
    return {'result': data}
